package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpSession session) {
        String username = employee.getUsername();
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        Employee employeeByUsername = employeeService.getOne(new QueryWrapper<Employee>().eq("username", username));
        if (employeeByUsername == null) {
            return R.error("登录失败");
        }
        if (employeeByUsername.getStatus() == 0) {
            return R.error("用户被禁用");
        }
        if (!employeeByUsername.getPassword().equals(password)) {
            return R.error("登录失败");
        }
        employeeByUsername.setPassword(null);
        session.setAttribute("employee", employeeByUsername);
        return R.success(employeeByUsername);
    }

    @PostMapping("/logout")
    public R logout(HttpSession session) {
        //让session失效
        session.invalidate();
        return R.success(null);
    }

    @PostMapping()
    public R save(@RequestBody Employee employee) {
        String s = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(s);
        employeeService.save(employee);
        return R.success("添加成功");
    }

    @RequestMapping("/page")
    public R<Page<Employee>> page(Integer page, Integer pageSize, String name) {

        Long currentThread = BaseContext.getCurrentThread();log.info("当前的用户ID为：{}",currentThread);


        Page<Employee> pages = new Page<>(page, pageSize);

        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<Employee>().like(!StringUtils.isEmpty(name), Employee::getName, name);
        Thread thread = Thread.currentThread();
        String threadName = thread.getName();

        log.info("threadName：{}",threadName);
        log.info("thread：{}",thread);

        employeeService.page(pages, queryWrapper);
        return R.success(pages);
    }

    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id) {
        Employee byId = employeeService.getById(id);
        return R.success(byId);
    }

    @PutMapping
    public R update(@RequestBody Employee employee, HttpSession session) {
        System.out.println("更新： employee = " + employee);
        employeeService.updateById(employee);
        return R.success(null);
    }
}
